package ui;

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import java.awt.*;
import java.awt.event.*;

/** Represents the ui for creating a user
 * @author Matthias
 * @author NielsChristianMouritsen
 */
public class AdmCreate extends JPanel {
	
	private JLabel lbl_Intro;
	private JLabel lbl_Firstname;
	private JLabel lbl_Lastname;
	private JLabel lbl_ID;
	private JLabel lbl_PIN;
	private JLabel lbl_Header;
	private JLabel lbl_Info1;
	private JLabel lbl_Info2;
	private JTextField textField_Firstname;
	private JTextField textField_Lastname;
	private JTextField textField_userID;
	private JPasswordField passwordField_PIN;
	private JButton btn_Back;
	private JButton btn_Submit;
	private JTextField textField_Streetname;
	private JTextField textField_ZipCode;
	private JLabel lbl_Streetname;
	private JLabel lbl_Streetnr;
	private JTextField textField_Streetnr;
	private JLabel lbl_ZipCode;
	private JTextField textField_City;
	private JLabel lbl_City;
	private JLabel lbl_Note;
	private JTextField textField_Tlf;
	private JLabel lbl_Tlf;
	private JLabel logo_ATM;
	
	/**
	 * 
	 */
	public AdmCreate() {
		
		setPreferredSize(new Dimension(600, 480));
		setLayout(null);
		
		lbl_Intro = new JLabel("Note: All fields are required in order to create a user");
		lbl_Intro.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Intro.setBounds(80, 90, 397, 21);
		add(lbl_Intro);
		
		lbl_Firstname = new JLabel("Firstname:");
		lbl_Firstname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Firstname.setBounds(287, 126, 82, 21);
		add(lbl_Firstname);
		
		lbl_Lastname = new JLabel("Lastname:");
		lbl_Lastname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Lastname.setBounds(287, 162, 81, 21);
		add(lbl_Lastname);
		
		lbl_ID = new JLabel("ID:");
		lbl_ID.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_ID.setBounds(90, 122, 22, 21);
		add(lbl_ID);
		
		lbl_PIN = new JLabel("PIN:");
		lbl_PIN.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_PIN.setBounds(80, 160, 33, 21);
		add(lbl_PIN);
		
		lbl_Header = new JLabel("Create User");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(225, 24, 144, 34);
		add(lbl_Header);
		
		lbl_Streetname = new JLabel("Streetname:");
		lbl_Streetname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Streetname.setBounds(276, 236, 93, 21);
		add(lbl_Streetname);
		
		lbl_Streetnr = new JLabel("Street nr.:");
		lbl_Streetnr.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Streetnr.setBounds(294, 274, 75, 21);
		add(lbl_Streetnr);
		
		lbl_ZipCode = new JLabel("Zip code:");
		lbl_ZipCode.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_ZipCode.setBounds(298, 310, 71, 21);
		add(lbl_ZipCode);
		
		lbl_City = new JLabel("City:");
		lbl_City.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_City.setBounds(333, 344, 36, 21);
		add(lbl_City);
		
		lbl_Note = new JLabel("<html><p>ID and PIN <br> are required for login </p></html>");
		lbl_Note.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		lbl_Note.setBounds(80, 317, 185, 48);
		add(lbl_Note);
		
		lbl_Tlf = new JLabel("Telephone:");
		lbl_Tlf.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Tlf.setBounds(282, 194, 87, 21);
		add(lbl_Tlf);
		
		btn_Back = new JButton("Back");
		btn_Back.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Back.setBounds(80, 395, 130, 50);
		add(btn_Back);
		
		btn_Submit = new JButton("Submit");
		btn_Submit.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Submit.setBounds(399, 395, 130, 50);
		add(btn_Submit);
			
		textField_Firstname = new JTextField();
		textField_Firstname.addMouseListener(mouseListener);
		textField_Firstname.setToolTipText("letters and whitespaces allowed");
		((AbstractDocument)textField_Firstname.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_Firstname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Firstname.setBounds(378, 120, 150, 30);
		add(textField_Firstname);
		textField_Firstname.setColumns(10);

		textField_Lastname = new JTextField();
		textField_Lastname.addMouseListener(mouseListener);
		textField_Lastname.setToolTipText("letters and whitespaces allowed");
		((AbstractDocument)textField_Lastname.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_Lastname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Lastname.setBounds(378, 156, 150, 30);
		add(textField_Lastname);
		textField_Lastname.setColumns(10);
		
		textField_userID = new JTextField();
		textField_userID.addMouseListener(mouseListener);
		textField_userID.setToolTipText("8 digits required");
		((AbstractDocument)textField_userID.getDocument()).setDocumentFilter(new LimitOfChar(8));
		textField_userID.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_userID.setBounds(115, 120, 150, 30);
		add(textField_userID);
		textField_userID.setColumns(10);
		
		passwordField_PIN = new JPasswordField();
		passwordField_PIN.addMouseListener(mouseListener);
		passwordField_PIN.setToolTipText("4 digits required");
		((AbstractDocument)passwordField_PIN.getDocument()).setDocumentFilter(new LimitOfChar(4));
		passwordField_PIN.setFont(new Font("SansSerif", Font.PLAIN, 16));
		passwordField_PIN.setBounds(115, 156, 150, 30);
		add(passwordField_PIN);

		textField_Streetname = new JTextField();
		textField_Streetname.addMouseListener(mouseListener);
		textField_Streetname.setToolTipText("letters and whitespaces allowed");
		textField_Streetname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Streetname.setBounds(378, 232, 150, 30);
		add(textField_Streetname);
		textField_Streetname.setColumns(10);
		
		textField_ZipCode = new JTextField();
		textField_ZipCode.addMouseListener(mouseListener);
		textField_ZipCode.setToolTipText("4-5 digits required");
		((AbstractDocument)textField_ZipCode.getDocument()).setDocumentFilter(new LimitOfChar(5));
		textField_ZipCode.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_ZipCode.setBounds(378, 304, 150, 30);
		add(textField_ZipCode);
		textField_ZipCode.setColumns(10);
		
		textField_Streetnr = new JTextField();
		textField_Streetnr.addMouseListener(mouseListener);
		textField_Streetnr.setToolTipText("letters, digits, whitespaces and commas allowed");
		((AbstractDocument)textField_Streetnr.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_Streetnr.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Streetnr.setBounds(378, 268, 150, 30);
		add(textField_Streetnr);
		textField_Streetnr.setColumns(10);
		
		textField_City = new JTextField();
		textField_City.addMouseListener(mouseListener);
		textField_City.setToolTipText("letters and whitespaces allowed");
		((AbstractDocument)textField_City.getDocument()).setDocumentFilter(new LimitOfChar(30));
		textField_City.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_City.setBounds(379, 340, 150, 30);
		add(textField_City);
		textField_City.setColumns(10);
		
		textField_Tlf = new JFormattedTextField();
		textField_Tlf.addMouseListener(mouseListener);
		textField_Tlf.setToolTipText("8-15 digits required");
		((AbstractDocument)textField_Tlf.getDocument()).setDocumentFilter(new LimitOfChar(15));
		textField_Tlf.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Tlf.setBounds(378, 192, 150, 30);
		add(textField_Tlf);
		textField_Tlf.setColumns(10);

		lbl_Info1 = new JLabel("<html><p>Note: Hover over the fields for requirements </p></html>");
		lbl_Info1.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		lbl_Info1.setBounds(82, 209, 184, 48);
		add(lbl_Info1);
		
		lbl_Info2 = new JLabel("<html><p>No foreign characters <br> or symbols </p></html>");
		lbl_Info2.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		lbl_Info2.setBounds(80, 268, 171, 46);
		add(lbl_Info2);
		
		logo_ATM = new JLabel("");
		logo_ATM.setIcon(new ImageIcon(AdmCreate.class.getResource("/pic/LOGO Mini.png")));
		logo_ATM.setBounds(15, 15, 50, 50);
		add(logo_ATM);
	}
		
	/** Adds action listeners
	 * @param event
	 */
	public void addActionListener(ActionListener event) {
		btn_Submit.addActionListener(event);
		btn_Back.addActionListener(event);
	}
	
	/** 
	 * @return back button
	 */
	public JButton getBack(){
		return btn_Back;
	}
	
	/**
	 * @return submit button
	 */
	public JButton getSubmit(){
		return btn_Submit;
	}
		
	/**
	 * Clears the text fields and the password field on startup
	 */
	public void clearFields(){
		textField_userID.setText("");
		passwordField_PIN.setText("");
		textField_Firstname.setText("");
		textField_Lastname.setText("");
		textField_Streetname.setText("");
		textField_Streetnr.setText("");
		textField_ZipCode.setText("");	
		textField_City.setText("");
		textField_Tlf.setText("");
	}

	/**
	 * @return entered first name 
	 */
	public String getTextField_Firstname() {
		return textField_Firstname.getText().trim();
	}

	/**
	 * @return entered last name
	 */
	public String getTextField_Lastname() {
		return textField_Lastname.getText().trim();
	}

	/**
	 * @return entered user ID
	 */
	public String getTextField_userID() {
		return textField_userID.getText().trim();
	}

	/**
	 * @return entered password (pin)
	 */
	public int getPasswordField_PIN() {
		char[] pin = passwordField_PIN.getPassword();
		return Integer.parseInt(new String(pin));
	}

	/**
	 * @return entered street name 
	 */
	public String getTextField_Streetname() {
		return textField_Streetname.getText().trim();
	}

	/**
	 * @return entered zip-code
	 */
	public String getTextField_Zipcode() {
		return textField_ZipCode.getText().trim();
	}

	/**
	 * @return entered street number
	 */
	public String getTextField_Streetnr() {
		return textField_Streetnr.getText().trim();
	}
	
	/**
	 * @return entered city
	 */
	public String getTextField_City() {
		return textField_City.getText();
	}
	
	/**
	 * @return telephone number
	 */
	public String getTextField_Tlf(){
		return textField_Tlf.getText().trim();
	}
	
	/** Adding a mouselistener to the class, by instantiating a MouseAdapter in an anonymous class 
	 * When the mouse is hovering over an object, the tooltip is enabled
	 * When the mouse is not hovering over an object, the tooltop is disabled
	 */
	MouseListener mouseListener = new MouseAdapter(){    
	    final int defaultTimeout = ToolTipManager.sharedInstance().getInitialDelay();

	    @Override
	    public void mouseEntered(MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(0);
	    }

	    @Override
	    public void mouseExited(MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(defaultTimeout);
	    }
	};
	
} // end class AdmCreate